_stringExpr = re.compile(u'(?:\\\\x(?P<unicode>[a-fA-F0-9]{2})) # Match hex-escaped unicode\n|\n(?:\\\\u(?P<unicode2>[a-fA-F0-9]{4})) # Match hex-escaped high unicode\n|\n(?P<control>\\\\[fbntr\\\\"]) # Match escaped control characters\n', re.VERBOSE)
_controlMap = {
u'\\f': u'\x0c',
u'\\b': u'\x08',
u'\\n': u'\n',
u'\\t': u'\t',
u'\\r': u'\r',
u'\\"': u'"',
u'\\\\': u'\\' }
def _stringSub(m):
u = m.group('unicode')
if u is None:
u = m.group('unicode2')
if u is not None:
return unichr(int(u, 16))
c = m.group('control')
return _controlMap[c]
def parseString(tokens):
if type(tokens[0]) is not StringToken:
raise ParseError, 'Unexpected %r' % tokens[0]
type(tokens[0]) is not StringToken
s = _stringExpr.sub(_stringSub, tokens.pop(0)[1:-1].decode('utf-8'))
return (s, tokens)
def parseIdentifier(tokens):
if type(tokens[0]) is not IdentifierToken:
raise ParseError('Unexpected %r' % (tokens[0],))
type(tokens[0]) is not IdentifierToken
return (tokens.pop(0), tokens)
def parseList(tokens):
l = []
tokens.pop(0)
first = True
while tokens[0] != ']':
if not first:
accept(',', tokens)
first = False
(value, tokens) = parseValue(tokens)
l.append(value)
accept(']', tokens)
return (l, tokens)
def parseObject(tokens):
o = { }
tokens.pop(0)
first = True
while tokens[0] != '}':
if not first:
accept(',', tokens)
first = False
(name, tokens) = parseString(tokens)
accept(':', tokens)
(value, tokens) = parseValue(tokens)
o[name] = value
accept('}', tokens)
return (o, tokens)
def parse(s):
'''
Return the object represented by the JSON-encoded string C{s}.
'''
tokens = tokenise(s)
(value, tokens) = parseValue(tokens)
if tokens:
raise ParseError, 'Unexpected %r' % tokens[0]
tokens
return value
class CycleError(Exception):
pass
_translation = []([ (o, u'\\x%02x' % (o,)) for o in range(32) ])